package com.yonyou.jrmob.application.ui.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hss01248.dialog.StyledDialog;
import com.yonyou.jrmob.application.R;

import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.StatusBarUtil;
import jrmob.yonyou.com.commonlib.baseui.base.BaseActivityMob_lib;

public class WebviewActivity extends BaseActivityMob_lib {

    public static final String INTENT_URL = "INTENT_URL";
    private String url;//目标页的url

    private Dialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview_lry);
        StatusBarUtil.setStatusBarColor(this,R.color.login_titlebar);
        initData();
        initView();
    }

    @SuppressLint("JavascriptInterface")
    @Override
    public void initView() {
        WebView webView = (WebView) findViewById(R.id.webview_lry);
        final WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(
                    WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                loadingDialog= StyledDialog.buildLoading().setCancelable(false,false).show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                StyledDialog.dismissLoading();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                StyledDialog.dismissLoading();
                showToast("网络错误，请重试!");
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                // android 5.0以上默认不支持Mixed Content
                handler.proceed();
            }
        });
        webView.setWebChromeClient(new WebChromeClient());
       /* webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                //return super.onJsAlert(view, url, message, result);
                result.cancel();
                return true;
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                return super.onJsConfirm(view, url, message, result);
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
                return super.onJsPrompt(view, url, message, defaultValue, result);
            }
        });*/
        webView.addJavascriptInterface(WebviewActivity.this,"jsObject");
        webView.loadUrl(url);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        if (intent == null){
            return;
        }
        url = intent.getStringExtra(INTENT_URL);
        Log.e("URL",url);
    }
    /*关闭本activity*/
    @JavascriptInterface
    public void finishWebviewActivity(){
        finish();
    }
}
